/**
 * 
 */
package org.neptune.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author liuwenlai
 */
public class DateUtil {

	public final static String format1 = "yyyy-MM-dd";

	public final static String format2 = "yyyy-MM-dd HH:mm:ss";

	public static void main(String[] args) {
		Date date = geLastWeekMonday(new Date(), 1);
		Date date1 = geLastWeekMonday(new Date(), 0);
		System.out.println(date);
		System.out.println(date1);

		Date date2 = getmon(-1, 1);
		Date date3 = getmon(-1, 0);
		System.out.println("date2" + date2);
		System.out.println(date3);
		Date date4 = getSecond(-900);
		System.out.println("date4" + date4);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		int dayMonth = cal.get(Calendar.DAY_OF_MONTH);
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(new Date());
		cal1.add(Calendar.SECOND, 3600);
		System.out.println(cal1.getTime() + "   dayWeek");
		System.out.println(dayWeek + "   dayWeek");
		System.out.println(dayMonth + "   dayMonth");
	}

	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek){
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}

	public static Date geLastWeekMonday(Date date, int unit) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, unit);
		return cal.getTime();
	}

	public static Date getmon(int amount, int unit) {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, amount);
		cale.set(Calendar.DAY_OF_MONTH, unit);
		return cale.getTime();
	}

	public static String getNowDayStr() {
		SimpleDateFormat format = new SimpleDateFormat(format2);
		return format.format(new Date());
	}

	public static Date getSecond(int amount) {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.SECOND, amount);
		return cale.getTime();
	}
}
